package com.rerun.phone.park;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.CollectionResume;
import com.rerun.entity.JobIntention;
import com.rerun.entity.MemberUser;
import com.rerun.entity.Resume;
import com.rerun.entity.vo.ParkResumeVo;
import com.rerun.enums.ResumeStateEnum;
import com.rerun.service.*;
import com.rerun.vo.park.*;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * The type Park resume controller.
 */
@Api(value = "园长端-简历相关", description = "园长端-简历相关")
@RestController
@RequestMapping(value = "/parkApi/resume")
public class ParkResumeController extends SuperController {
	/**
	 * The Resume service.
	 */
	@Autowired
	IResumeService iResumeService;
	/**
	 * The Collection resume service.
	 */
	@Autowired
	ICollectionResumeService collectionResumeService;
	/**
	 * The Mail service.
	 */
	@Autowired
	IMailService iMailService;
	/**
	 * The Feedback service.
	 */
	@Autowired
	IJobFeedbackService feedbackService;
	@Autowired
	IChatRecordsService chatRecordsService;
	@Autowired
	IJobIntentionService intentionService;
	@Autowired
	IMemberUserService memberUserService;
	/**
	 * 找牛人列表
	 *
	 * @param resumeSearchVo the resume search vo
	 * @param pageNumber     the page number
	 * @return rest
	 */
	@ApiOperation(value = "找牛人列表", notes = "找牛人列表", httpMethod = "POST")
	@RequestMapping(value = "/list/{pageNumber}")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "jobName",required = false, value = "职位名称",paramType ="query"),
		 @ApiImplicitParam(name = "edu",required = false, value = "学历",paramType ="query"),
		 @ApiImplicitParam(name = "experience",required = false, value = "工作经验",paramType ="query"),
		 @ApiImplicitParam(name = "hopePay",required = false, value = "期望薪资",paramType ="query"),
		 @ApiImplicitParam(name = "latest",required = false, value = "最新",paramType ="query"),
		 @ApiImplicitParam(name = "recommend",required = false, value = "推荐",paramType ="query")
	})
	@ResponseExceptionReturn
	public Rest list(@ModelAttribute ParkResumeSearchVo resumeSearchVo,@ApiParam(value="页码",required=true) @PathVariable Integer pageNumber) {
		Page<ParkResumeVo> page = getPage(pageNumber);
		Page<ParkResumeVo> data = iResumeService.selectResumeListPage(resumeSearchVo, page);
		return Rest.okData(data.getRecords());
	}

	/**
	 * 简历收藏列表
	 *
	 * @param parkId     the park id
	 * @param pageNumber the page number
	 * @return rest
	 */
	@ApiOperation(value = "简历收藏列表", notes = "简历收藏列表", httpMethod = "GET")
	@RequestMapping(value = "/collectList/{parkId}/{pageNumber}")
	@ResponseExceptionReturn
	public Rest collectResumeList(@PathVariable String parkId, @PathVariable Integer pageNumber) {
		if (StringUtils.isNoneEmpty(parkId)) {
			Page<ParkResumeVo> page = getPage(pageNumber);
			Page<ParkResumeVo> data = iResumeService.selectCollectResumesPage(page,parkId);
			return Rest.okData(data.getRecords());
		}
		return Rest.failure("参数不能为空");
	}

	/**
	 * 收藏简历
	 *
	 * @param parkId   the park id
	 * @param memberId the member id
	 * @return rest
	 */
	@ApiOperation(value = "收藏简历", notes = "收藏简历", httpMethod = "POST")
	@RequestMapping(value = "/collect/{parkId}/{memberId}")
	@ResponseExceptionReturn
	public Rest collectResume(@PathVariable String parkId,@PathVariable String memberId) {
		EntityWrapper<CollectionResume> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("parkId", parkId);
		entityWrapper.eq("memberId", memberId);
		List<CollectionResume> collectionResumes = collectionResumeService.selectList(entityWrapper);
		if(collectionResumes == null|| collectionResumes.size() < 1) {
			CollectionResume collectionResume = new CollectionResume();
			collectionResume.setCreateTime(new Date());
			collectionResume.setParkId(parkId);
			collectionResume.setMemberId(memberId);
			boolean b = collectionResumeService.insert(collectionResume);
			if(b)
				return Rest.ok("ok");
		}
		
		return Rest.failure("已收藏简历");
	}
	@ApiOperation(value = "取消收藏简历", notes = "收藏简历", httpMethod = "POST")
	@RequestMapping(value = "/unCollect/{parkId}/{memberId}")
	@ResponseExceptionReturn
	public Rest unCollectResume(@PathVariable String parkId,@PathVariable String memberId) {
		EntityWrapper<CollectionResume> entityWrapper = new EntityWrapper<>();
		if(StringUtils.isNoneEmpty(parkId)&&StringUtils.isNoneEmpty(memberId)) {
			entityWrapper.eq("parkId", parkId);
			entityWrapper.eq("memberId", memberId);
			try {
				collectionResumeService.delete(entityWrapper);
				return Rest.ok();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return Rest.failure("取消收藏失败");
	}
	/**
	 * 收简历
	 *
	 * @param parkId     the park id
	 * @param pageNumber the page number
	 * @return rest
	 */
	@ApiOperation(value = "收到投递简历", notes = "收到投递简历", httpMethod = "GET")
	@RequestMapping(value = "/take/{parkId}/{pageNumber}")
	@ResponseExceptionReturn
	public Rest receivedResume(@PathVariable String parkId, @PathVariable Integer pageNumber) {
		if (StringUtils.isNoneEmpty(parkId)) {
			Page<ReceivedResumeVo> page = getPage(pageNumber);
			Page<ReceivedResumeVo> data = iResumeService.receivedResume(parkId,page);
			return Rest.okData(data.getRecords());
		}
		return Rest.failure("参数不能为空");
	}

	/**
	 * 待面试
	 *
	 * @param parkId     the park id
	 * @param pageNumber the page number
	 * @param status     the status
	 * @return rest
	 */
	@ApiOperation(value = "收到简历API", notes = "支持查询已投递，待面试，以拒绝，全部数据", httpMethod = "GET")
	@RequestMapping(value = "/forInterview/{parkId}/{pageNumber}")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "status",required = false, value = "100(投递),200(待面试),400(拒绝),无参数加载全部状态数据",paramType ="query")
	})
	@ResponseExceptionReturn
	public Rest forTheInterView(@PathVariable String parkId, @PathVariable Integer pageNumber,String status) {
		if (StringUtils.isNoneEmpty(parkId)) {
			Page<InteviewResumeVo> page = getPage(pageNumber);
			Page<InteviewResumeVo> data = iResumeService.forTheInterview(status,parkId,page);
			return Rest.okData(data.getRecords());
		}
		return Rest.failure("参数不能为空");
	}

	/**
	 * 面试邀请
	 *
	 * @param jobId       the job id
	 * @param memberId    the member id
	 * @param interviewVo the interview vo
	 * @return rest
	 */
	@ApiOperation(value = "面试邀请", notes = "面试邀请", httpMethod = "GET")
	@RequestMapping(value = "interview/{jobId}/{memberId}")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "interviewTime",required = true, value = "面试时间 传值eg:2017-10-15 9:30",paramType ="query"),
		 @ApiImplicitParam(name = "remark",required = false, value = "备注",paramType ="query")
	})
	public Rest interView(HttpServletRequest request, @PathVariable String jobId, @PathVariable String memberId, InterviewVo interviewVo) {
		
		try {
			//更新求职反馈信息
			interviewVo.setStatus(ResumeStateEnum.interview.getKey());
			boolean b = feedbackService.updateStatus(interviewVo);
			if(!b)return Rest.failure("面试邀请失败，请重试");
			
			return iMailService.interviewInvitationEmail(request, jobId, memberId, interviewVo.getInterviewTime(), interviewVo.getRemark());
		} catch (IOException | MessagingException e) {
			e.printStackTrace();
			return Rest.failure("发送面试邀请失败，请重试");
		}
	}

	/**
	 * 面试状态
	 *
	 * @param interviewVo the interview vo
	 * @return rest
	 */
	@ApiOperation(value = "面试状态(拒绝面试，通过面试)", notes = "面试状态(拒绝面试，通过面试)", httpMethod = "POST")
	@RequestMapping(value="/refuseInterview")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "memberId",required = true, value = "用户id",paramType ="query"),
		 @ApiImplicitParam(name = "jobId",required = true, value = "jobId",paramType ="query"),
		 @ApiImplicitParam(name = "status",required = false, value = "101(查看),300(通过),400(拒绝)",paramType ="query"),
		 @ApiImplicitParam(name = "remark",required = false, value = "备注/拒绝理由",paramType ="query")
	})
	public Rest refuseResume(@ModelAttribute InterviewVo interviewVo) {
		interviewVo.setStatus(ResumeStateEnum.pass.getKey());
		if("".equals(interviewVo.getRemark())) {
			interviewVo.setRemark("拒绝面试");
		}
		try {
			boolean b = feedbackService.updateStatus(interviewVo);
			if(b)
				return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("拒绝面试请求失败");
	}

	/**
	 * 查看简历
	 *
	 * @param memeberId the memeber id
	 * @return rest
	 */
	@ApiOperation(value = "查看简历", notes = "查看简历", httpMethod = "GET")
	@RequestMapping(value = "/look/{id}")
	@ResponseExceptionReturn
	public Rest lookResume(@ApiParam(value = "memberId", required = true) @PathVariable("id") String memeberId) {
		if (StringUtils.isNoneEmpty(memeberId)) {
			Resume resume = iResumeService.lookResume(memeberId);
			return Rest.okData(resume);
		}
		return Rest.failure("参数不能为空");
	}
	@ApiOperation(value = "查看简历-求职意向", notes = "查看简历-求职意向", httpMethod = "GET")
	@RequestMapping(value = "/lookJobIntention/{memberid}")
	@ResponseExceptionReturn
	public Rest lookResumeJobIntention(@ApiParam(value = "memberId", required = true) @PathVariable("memberid") String memeberId) {
		if (StringUtils.isNoneEmpty(memeberId)) {
			EntityWrapper<JobIntention> arg0 = new EntityWrapper<>();
			arg0.eq("memberId", memeberId);
			JobIntention intention = intentionService.selectOne(arg0);
			String highestEdu = "";
			MemberUser memberUser = memberUserService.selectById(memeberId);
			if(memberUser != null)highestEdu = memberUser.getHighestEdu();
			intention.setHighestEdu(highestEdu);
			return Rest.okData(intention);
		}
		return Rest.failure("参数不能为空");
	}
	@ApiOperation(value = "查看简历是否被收藏", notes = "查看简历是否被收藏", httpMethod = "GET")
	@RequestMapping(value = "/resumeIsCollection/{parkId}/{memberid}")
	@ResponseExceptionReturn
	public Rest resumeIsCollection(@ApiParam(value = "parkId", required = true) @PathVariable("parkId") String parkId,@ApiParam(value = "memberId", required = true) @PathVariable("memberid") String memeberId) {
		if (StringUtils.isNoneEmpty(memeberId)) {
			EntityWrapper<CollectionResume> arg0 = new EntityWrapper<>();
			arg0.eq("memberId", memeberId);
			CollectionResume collectionResume = collectionResumeService.selectOne(arg0);
			String status = "yes";
			if(collectionResume == null)status = "no";
			return Rest.ok(status);
		}
		return Rest.failure("参数不能为空");
	}
	/**
	 * 园长端获取收简历的数量
	 * @return
	 */
	@ApiOperation(value = "园长端获取收简历的数量", notes = "面试状态(拒绝面试，通过面试)", httpMethod = "POST")
	@RequestMapping(value="/getReceivedResumeNum/{parkId}/{status}")
	@ResponseExceptionReturn
	public Rest getReceivedResumeNum(@ApiParam(value = "parkId", required = true) @PathVariable("parkId") String parkId,@ApiParam(value = "101(查看),300(通过),400(拒绝)", required = false) @PathVariable("status") String status) {
		int num = iResumeService.getResumeStatusNum(parkId,status);
		return Rest.okData(num);
	}
	
	@ApiOperation(value = "已沟通简历", httpMethod = "GET", notes = "已沟通简历列表")
	@RequestMapping(value = "/chat/{parkId}/{pageNumber}", method = RequestMethod.GET)
	public Rest list(@PathVariable String parkId, @PathVariable Integer pageNumber) {
		try {
			Page<ParkResumeViewVo> page = getPage(pageNumber);
			page = chatRecordsService.selectParkResumeListPage(parkId, page);
			return Rest.okData(page.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("获取列表失败");
	}
}
